<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('confirm','cancel','agree','disagree');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
}
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'当前商品列表'),
	array('op'=>'edit','title'=>'添加商品_ADD Goods'),
);
$return['status']=array('0'=>'下架','1'=>'上架','2'=>'售罄','3'=>'回收站');
//上架商品
if($_RQ['op']=='list'){
	$params=array('type <>'=>4);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['pcate'])){
		$params['pcate']=intval($_RQ['pcate']);
		$this->session('pcate',intval($_RQ['pcate']));
		$return['params']['pcate']=intval($_RQ['pcate']);
	}
	$tip='【温馨提示】：所属商户如果为“无”，表示商户已经不存在！排序，越大越靠前！<red>商品价格解释：如果门店设置了商品价格，将以门店价格为准！规格的库存，价格也是以门店的为准！</red>';
	$params['onepluginname']=$this->plugin_name;
	$install = check_permission('jy_poster');
	if(!$install){
		$tip .='</br><red>【注意】：还未安装《活动海报》！商品详情不显示生成海报按钮！</red>';
	}
	$tip .='</br>【<red>一键更新商品权限</red>】：是指新版多选权限格式的更新（之前是单选）</br>[温馨提示]：设为公共分类后，该分类其他场景共享！';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','merchid','pluginname','onepluginname','star','uniacid','paixu','title','image','pcate','ccate','scate','price','status','createtime','total','salenum','unit','saletype');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','paixu desc,star desc, saletype desc, id desc');
	if($list){
		if($this->plugin_exists('jy_merch')){
			$jy_merch=true;
		}
		foreach($list as &$item){
				$item['title']=cfc('tools')->substr($item['title'],0,$substrlength);
				$item['pcatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate']),'title');
				$item['ccatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['ccate']),'title');
				$item['scatetitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['scate']),'title');				
				$item['url']=custom_url($this->plugin_name.'/goods.detail//id='.$item['id'],true);
				$item['salenum2']=pdo_getsum('jy_weishop_order_goods',array('goodsid'=>$item['id']),'total')?:0;
				if($jy_merch){
					$merch=pdo_get('jy_merch_user',array('id'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid']),array('id','merchname','logo','salecate'));
				}
				if(!$merch['merchname'] && $item['merchid']){
					$merch['merchname']='商户不存在';
				}
				$item['merchlogo']=$merch['logo'];
				$item['salecate']=$merch['salecate']?:'';
				$item['oneplugintitle']=$item['onepluginname']?pdo_getcolumn('core_plugins',array('name'=>$item['onepluginname']),'title'):'电商客';
				$item['merchname']=$merch['merchname']?:'平台自营';
				$item['salecate']=$merch['salecate']?:'无';
				$item['unit']=$item['unit']?'/'.$item['unit']:'/件';
				$item['pcatetitle']=$item['pcatetitle']?$item['pcatetitle']:'无';
				$item['ccatetitle']=$item['ccatetitle']?$item['ccatetitle']:'无';
				$item['scatetitle']=$item['scatetitle']?:'无';
				if($item['salenum']>=10000){
					$wans=$item['salenum']/10000;
					$wans=sprintf('%.1f',$wans);
					$item['salenum']=$wans.'万';
				}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序_The sorting','type'=>'string'),
			array('name'=>'star','title'=>'星数_Star','type'=>'star','set'=>'star'),
			array('type'=>'img_string','title'=>'所属商户_Merch title',
			'data'=>array(
				array('title'=>'名称：_Merch：','name'=>'merchname'),
				array('title'=>'项目：_Cate：','name'=>'salecate')
				)
			),
			array('type'=>'img_string','title'=>'商品名称_Goods title','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'场景：_Legao：','name'=>'oneplugintitle')
				)
			),
			array('type'=>'img_string','title'=>'商品分类_Goods group',
			'data'=>array(
				array('title'=>'一级：_Level 1：','name'=>'pcatetitle'),
				array('title'=>'二级：_Level 2：','name'=>'ccatetitle'),
				array('title'=>'三级：_Level 3：','name'=>'scatetitle')
				)
			),
			array('type'=>'img_string','title'=>'库存/销量_Inventory/Sales',
			'data'=>array(
				array('title'=>'库存：_Inventory：','name'=>'total'),
				array('title'=>'销量：_Sales：','name'=>'salenum2','unit'=>'unit')
				)
			),
			array('name'=>'price','title'=>'价格(点击编辑↓)_Price','type'=>'string','set'=>'set'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'saletype','title'=>'商品属性_attribute','type'=>'checkbox','options'=>'saletypes'),
			array('name'=>'url','title'=>'复制链接_Copy Link','type'=>'copy'),
		),'action'=>array(
			array('type'=>'href','action'=>'activity.buyagain.list','title'=>'设置活动_Set activity'),
			array('type'=>'post','op'=>'copy','title'=>'复制商品_Copy'),
		),'batch'=>array(
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus0/','title'=>'一键下架_Shelves'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus2/','title'=>'一键售罄_Sold out'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/setstatus3/','title'=>'一键加入回收站_Recycle'),
			array('type'=>'post','url'=>'jy_weishop/goods.list/upseven/','title'=>'一键支持退款_Uptask'),
			array('type'=>'modal','url'=>'jy_weishop/goods.list/uppirce/','title'=>'批量修改价格'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'type'=>'string','title'=>'商品名称_Goods'),
			array('name'=>'pcate','width'=>15,'type'=>'select','title'=>'商品分类_Groups','options'=>'groups'),
			array('name'=>'saletype','width'=>15,'type'=>'radio','title'=>'属性_Saletype','options'=>'saletypes'),
		)
	);
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'isshow'=>1,'parentid'=>0);
	$groupspram['onepluginname']=$this->plugin_name;
	$return['groups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('id','title'),'id');
	$return['type']=$this->goods_types;
	$return['saletypes']=$this->saletypes;
	$return['htmls']=$htmls;
	if($this->plugin_exists('jy_merch')){
		$merchs=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'),'id');
	}else{
		$merchs=array();
	}
	$merchs['0']=array('id'=>0,'merchname'=>'平台自营_Proprietary');
	$return['merchs']=$merchs;
	set_html('title','商品列表');
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['price'])){
		$update['price']=round($post['price'],2);
	}
	if($update){
		pdo_update('jy_weishop_goods',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	$return=model('jy_weishop/setgoods')->edit($id,'jy_mcaigou');
	if($return['item']['api_pluginname']=='jy_supplier'){
		$return['item']['supplier_id']=$return['item']['api_id'];
	}
	if($this->plugin_exists('jy_supplier')){
		if($return['item']['supplier_id']){
			$index=count($return['htmls']['edits'][0]['list']);
			$index--;
			unset($return['htmls']['edits'][0]['list'][$index]);
		}
		$return['htmls']['edits'][0]['list'][]=array('name'=>'supplier_id','title'=>'所属供货商','type'=>'select','options'=>'suppliers','help'=>'由供货商发布的商品！');
		$return['suppliers']=pdo_getall('jy_supplier_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'),'id');
		foreach($return['suppliers'] as &$sv){
			$sv['title']=$sv['merchname'];
		}
	}
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=model('jy_weishop/setgoods')->postdata($postdata);
	$data['actionurl']='jy_mcaigou/goods.detail/';
	$goodsid=intval($postdata['id']);
	if($goodsid){
		if(!$postdata['pluginname']){
			$data['pluginname']=$this->plugin_name;
		}
		pdo_update('jy_weishop_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['uniacid']=$_SESSION['uniacid'];
		$data['pluginname']=$this->plugin_name;
		$data['onepluginname']=$this->plugin_name;
		pdo_insert('jy_weishop_goods',$data);
		$goodsid=pdo_insertid();
	}
	model('jy_weishop/setgoods')->postup($postdata,$goodsid);
	exi('操作成功！','','goback');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	if(count($del)==0){
		exi('请先选择商品','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}